<template>
  <div v-loading="loading" class="login">
    <div class="box">
      <div class="title">咨询管理系统</div>
      <el-form :model="userInfo">
        <el-form-item label="账 号：" class="aa">
          <el-input v-model="userInfo.telephone" class="biaodan"></el-input>
        </el-form-item>
        <el-form-item label="密 码：" prop="pass" class="aa">
          <el-input type="password" v-model="userInfo.password" autocomplete="off" class="biaodan"></el-input>
        </el-form-item>
        <el-checkbox class="check" v-model="checked">记住密码</el-checkbox>
        <el-form-item align="center">
          <el-button
              class="btn"
              type="primary"
              @click="login"
              id="TencentCaptcha"
              data-appid="2058774045"
              data-cbfn="callbackName">登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
const Base64 = require('js-base64').Base64;
export default {
  name: "login",
  data() {
    return {
      loading: false,
      userInfo: {
        telephone: '',
        password: ''
      },
      checked: false
    }
  },
  created() {
    window.callbackName = this.login
    this.checked = JSON.parse(localStorage.getItem('p'))
    if (this.checked) {
      this.userInfo = {
        telephone: Base64.decode(localStorage.getItem(Base64.encode('telephone'))),
        password: Base64.decode(localStorage.getItem(Base64.encode('password')))
      }
    }
  },
  methods: {
    async login(res) {
      if (res.ret === 0) {
        this.loading = true;
        await this.$store.dispatch('login', this.userInfo).catch(() => {
          this.loading = false
        })
        await this.$router.push('/home/index')
        this.loading = false
        localStorage.setItem('p', this.checked)
        if (this.checked) {
          //存之前加密
          const tv = Base64.encode(this.userInfo.telephone)//加密手机号
          const pv = Base64.encode(this.userInfo.password)//加密密码
          const ti = Base64.encode('telephone')//加密telephone
          const pi = Base64.encode('password')//加密password
          localStorage.setItem(ti, tv)
          localStorage.setItem(pi, pv)
        }
      }
    }
  }
}
</script>

<style scoped>
.login {
  width: 100%;
  height: 100vh;
  background-image: url('https://gw.alipayobjects.com/zos/rmsportal/TVYTbAXWheQpRcWDaDMu.svg');
}

.title {
  margin-bottom: 25px;
  width: 100%;
  height: 35px;
  line-height: 35px;
  text-align: center;
  font-size:35px;

}
.check{
  padding-left: 70px;
}
.box {
  position: absolute;
  left: 500px;
  top: 180px;
  padding: 20px;
  height: 200px;
  width: 500px;
  margin: 0px auto;
}

.aa {
  padding-left: 11px;
}
.biaodan {
  width: 350px;
}
.btn {
  margin-left: -5px;
  width:350px;
  margin-top: 10px;
}
</style>